{% extends 'base.html' %}
{% from 'bootstrap/pagination.html' import render_pagination %}

{% block title %}评论管理{% endblock %}

{% block content %}
    <div class="page-header">
        <h2>
            {% if request.args.get('filter', 'unread') == 'unread' %}未读评论{% endif %}
            {% if request.args.get('filter') == 'all' %}全部评论{% endif %}
            {% if request.args.get('filter') == 'admin' %}来自管理员的评论{% endif %}
            <small class="text-muted">{{ pagination.total }}</small>
        </h2>

        <div>
        <ul class="nav nav-pills">
            <li class="nav-item">
                <a class="nav-link {% if request.args.get('filter', 'unread') == 'unread' %}active{% endif %}"
                   href="{{ url_for('admin.manage_comment', filter='unread') }}">未读 {% if unread_comments_count %}<span
                        class="badge badge-success">{{ unread_comments_count }}</span>{% endif %}</a>
            </li>
            <li class="nav-item">
                <a class="nav-link {% if request.args.get('filter') == 'all' %}active{% endif %}"
                   href="{{ url_for('admin.manage_comment', filter='all') }}">全部</a>
            </li>
            <li class="nav-item">
                <a class="nav-link {% if request.args.get('filter') == 'admin' %}active{% endif %}"
                   href="{{ url_for('admin.manage_comment', filter='admin') }}">来自管理员</a>
            </li>

            <li class="nav-item" style="position: absolute; right: 14%;">
                <form class="inline" method="post"
                        action="{{ url_for('.read_all_comment', next=request.full_path) }}">
                    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
                    <button type="submit" class="btn btn-warning">全部已读</button>
                </form>
            </li>
        </ul>
        </div>
    </div>

    {% if comments %}
        <table class="table table-striped">
            <thead>
            <tr>
                <th>序号</th>
                <th>作者</th>
                <th>内容</th>
                <th>发表日期</th>
                <th>操作</th>
            </tr>
            </thead>
            {% for comment in comments %}
                <tr {% if not comment.reviewed %}class="table-warning" {% endif %}>
                    <td>{{ loop.index + ((pagination.page - 1) * config['BLOG_COMMENT_PER_PAGE']) }}</td>
                    <td>
                        {% if comment.from_admin %}{{ admin.name }}{% else %}{{ comment.author }}{% endif %}<br>
                        
                        <a href="mailto:{{ comment.email }}">{{ comment.email }}</a>

                        {% if comment.from_admin %}
                            <span class="badge badge-primary">管理员</span>
                        {% endif %}
                    </td>
                    <td>{{ comment.body }}</td>
                    <td>{{ moment(comment.timestamp).format('LL') }}</td>
                    <td>
                        {% if not comment.read %}
                            <form class="inline" method="post"
                                  action="{{ url_for('.read_comment', comment_id=comment.id, next=request.full_path) }}">
                                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
                                <button type="submit" class="btn btn-success btn-sm">已读</button>
                            </form>
                        {% endif %}
                        <a class="btn btn-info btn-sm" href="{{ url_for('blog.show_post', post_id=comment.post.id) }}">查看文章</a>
                        <form class="inline" method="post"
                              action="{{ url_for('.delete_comment', comment_id=comment.id, next=request.full_path) }}">
                            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
                            <button type="submit" class="btn btn-danger btn-sm"
                                    onclick="return confirm('删除这条评论？');">删除
                            </button>
                        </form>
                    </td>
                </tr>
            {% endfor %}
        </table>
        <div class="page-footer">{{ render_pagination(pagination) }}</div>
    {% else %}
        <div class="tip"><h5>暂无评论</h5></div>
    {% endif %}
{% endblock %}
